#!/usr/bin/env python

import pygtk
pygtk.require('2.0')
import gtk
from configuration import Configuration
import DataStore

class Status:
    def delete_event(self, widget, event, data=None):
        gtk.main_quit()
        return False

    def click_callback(self, widget, data=None):
        configuration=Configuration()
    
    def __init__(self):
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.set_title("Thailand program status")
        self.window.set_size_request(800,250)
        self.window.connect("delete_event", self.delete_event)
        
        
        #create a box to house the frames with the liststore and buttons
        main_vbox = gtk.VBox(False, 5)
        main_vbox.set_border_width(10)
        self.window.add(main_vbox)
        
        #create a frame to hold the messages
        frame = gtk.Frame("Message Status")
        main_vbox.pack_start(frame, True, True, 0)
        
        #create a list Store with one string column to use as the model
        self.liststore = gtk.ListStore(str,str,str,str,str)
        self.treeview = gtk.TreeView(self.liststore)
        
        #create header column name
        self.tvcolumnFrom = gtk.TreeViewColumn('From')
        self.tvcolumnTo = gtk.TreeViewColumn('To')
        self.tvcolumnSubject = gtk.TreeViewColumn('Subject')
        self.tvcolumnDate = gtk.TreeViewColumn('Date')
        self.tvcolumnStatus = gtk.TreeViewColumn('Status')
        
        ds = DataStore.load("../test/")
        
        #load queued data
        dmsgs = ds.delivered_messages        
        
        for k, v in dmsgs.iteritems():
            self.liststore.append([dmsgs[k].sender, dmsgs[k].to, dmsgs[k].subject, dmsgs[k].date, 'Delivered'])          
        
        #load queued data
        msgs = ds.messages
        for k, v in msgs.iteritems():
            self.liststore.append([msgs[k].sender, msgs[k].to, msgs[k].subject, msgs[k].date, 'Queued'])        
        
        
        ################################
        #add the column to the treeview
        self.treeview.append_column(self.tvcolumnFrom)
        self.treeview.append_column(self.tvcolumnTo)
        self.treeview.append_column(self.tvcolumnSubject)
        self.treeview.append_column(self.tvcolumnDate)
        self.treeview.append_column(self.tvcolumnStatus)
        
        #render the data
        self.cellFrom = gtk.CellRendererText()
        self.cellTo = gtk.CellRendererText()
        self.cellSubject = gtk.CellRendererText()
        self.cellDate = gtk.CellRendererText()
        self.cellStatus = gtk.CellRendererText()
        
        #place self.CellFrom inside the tvcolumnFrom container
        self.tvcolumnFrom.pack_start(self.cellFrom, True)
        self.tvcolumnTo.pack_start(self.cellTo, True)
        self.tvcolumnSubject.pack_start(self.cellSubject, True)
        self.tvcolumnDate.pack_start(self.cellDate, True)
        self.tvcolumnStatus.pack_start(self.cellStatus, True)
        
        #set the cell attributes to the appropriate liststore columns
        self.tvcolumnFrom.add_attribute(self.cellFrom, 'text', 0)
        self.tvcolumnTo.add_attribute(self.cellTo, 'text', 1)
        self.tvcolumnSubject.add_attribute(self.cellSubject, 'text', 2)
        self.tvcolumnDate.add_attribute(self.cellDate, 'text', 3)
        self.tvcolumnStatus.add_attribute(self.cellStatus, 'text', 4)
        
        self.tvcolumnFrom.set_sort_column_id(0)
        self.tvcolumnTo.set_sort_column_id(1)
        self.tvcolumnSubject.set_sort_column_id(2)
        self.tvcolumnDate.set_sort_column_id(3)
        self.tvcolumnStatus.set_sort_column_id(4)
        
        self.treeview.set_reorderable(True)
        frame.add(self.treeview)
        
        #create the close button
        hbox = gtk.HBox(False, 0)
        main_vbox.pack_start(hbox, False, True, 0)
        
        #create a button linked to configuration page
        button=gtk.Button("Configuration")

        #connect the "clicked" signal of the button to our callback
        button.connect("clicked", self.click_callback, "configuration page")
        hbox.pack_start(button, True, True, 5)     
                
        button = gtk.Button("Close")
        button.connect("clicked", lambda w: gtk.main_quit())
        hbox.pack_start(button, True, True, 5)
        
        self.window.show_all()
        
def main():
    gtk.main()
    
if __name__ == "__main__":
    tvexample = Status()
    main()
              




